from rest_framework import serializers
from .models import User, Supplier, DeliveryStaff

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'role', 'email', 'phone']
        read_only_fields = ['id']

class SupplierSerializer(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = Supplier
        fields = ['id', 'user', 'company_name', 'business_license']
        read_only_fields = ['id']

class DeliveryStaffSerializer(serializers.ModelSerializer):
    user = UserSerializer()

    class Meta:
        model = DeliveryStaff
        fields = ['id', 'user', 'vehicle_number', 'service_area']
        read_only_fields = ['id']

class LoginSerializer(serializers.Serializer):
    username = serializers.CharField()
    password = serializers.CharField(write_only=True)

class RegisterSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True)
    
    class Meta:
        model = User
        fields = ['username', 'password', 'role']

    def create(self, validated_data):
        password = validated_data.pop('password')
        user = User(**validated_data)
        user.set_password(password)
        user.save()
        return user 